﻿using System;
using Commons;
using DAL;
using DAL.Enums;
using NUnit.Framework;
using SimpComm;
using ViewModel;
using log4net;
using System.Collections.Generic;

namespace Bll
{
    public class AdmService : BaseService<Adm>
    {
        public MsgBox Insert(AdmCreateModel addModel)
        {
            if (addModel.LoginID.IsNullOrWhiteSpace() || addModel.LoginPwd.IsNullOrWhiteSpace())
                return new MsgBox(false, "请输入密码.");
            var user = new Adm()
                           {
                               Security_LoginID = Util.AdmEncode(addModel.LoginID),
                               Security_LoginPwd = Util.AdmEncode(addModel.LoginPwd),
                               Name = addModel.Name,
                               Role = DAL.Enums.emAdmRole.Get(addModel.RoleStr).Value,
                               LivePhone = addModel.LivePhone,
                           };
            Action<Adm> defaultValueFun = p =>
                                       {
                                           p.ModifyDate = DateTime.Now;
                                           p.Wallet = 0.0m;
                                       };
            MsgBox msg = base.Insert(user, defaultValueFun);
            return msg;
        }


        public MsgBox Update(AdmEditModel editModel)
        {
            var user = new Adm();
            if (editModel.Security_LoginID.NotNullOrWhiteSpace())
                user.Security_LoginID = Util.AdmEncode(editModel.Security_LoginID);
            if (editModel.Security_LoginPwd.NotNullOrWhiteSpace())
                user.Security_LoginPwd = Util.AdmEncode(editModel.Security_LoginPwd);
            user.Name = editModel.Name;
            user.Role = DAL.Enums.emAdmRole.Get(editModel.RoleStr).Value;
            user.LivePhone = editModel.LivePhone;
            MsgBox msg = base.Update(p => p.ID == editModel.ID, p => user);
            return msg;
        }


        public MsgBox UpdateCols(AdmEditColsModel editModel)
        {
            if (editModel.ID > 0)
            {
                var a = Dal.GetEntity(p => p.ID == editModel.ID);
                if (a != null && a.ColKeys != editModel.AdmCol)
                {
                    return base.Update(p => p.ID == editModel.ID, p => new Adm() { ColKeys = editModel.AdmCol });
                }
                else
                {
                    return new MsgBox(false, "查询无记录或者传入值与原来相同");
                }
            }
            return new MsgBox(false, "请输入必要信息");
        }

        public DataInfo<AdmEditModel> GetEditView(Func<Adm, bool> func)
        {
            Func<Adm, AdmEditModel> select = p =>
                                                          {
                                                              return new AdmEditModel()
                                                                         {
                                                                             Name = p.Name,
                                                                             RoleStr = emAdmRole.Get(p.Role).Text,
                                                                             LivePhone = p.LivePhone,
                                                                         };
                                                          };
            return base.GetModel(func, select);
        }

        public MD<AdmListModel> GetMul_List(Func<Adm, bool> func)
        {
            var s = new SMP<Adm, AdmListModel>()
                       {
                           getmodel = p =>
                                                         {
                                                             return new AdmListModel()
                                                                        {
                                                                            ID = p.ID,
                                                                            Name = p.Name,
                                                                            Role = p.Role,
                                                                            Wallet = (float)(p.Wallet ?? 0),
                                                                            LivePhone = p.LivePhone,
                                                                        };
                                                         },
                           selected = func,
                       };
            return base.SM(s);
        }

    }


#if DEBUG
    [TestFixture]
    public class TestAdmService
    {
        public AdmService AdmService
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        [Test]
        public void Test_ChangePassword()
        {
            //var AdmService = new AdmService();
            ////取值
            //var data = AdmService.GetEntity(p => p.ID == 1);
            //var _old = data.Security_LoginPwd;
            //var _new = Randoms.GetRandomPureChar(8);
            //Assert.IsTrue(AdmService.ChangePassword(1, new AdmRequest()
            //                                     {
            //                                         OldPassord = _old,
            //                                         NewPassword = _new
            //                                     }));
            //data = AdmService.GetEntity(p => p.ID == 1);
            //Assert.AreEqual(data.Security_LoginPwd, _new);
        }
    }
#endif
}